<template>
  <div class="cart">
    <div class="goods-list" v-if="list.length > 0">
      <div class="goods" v-for="(data, i) in list" :key="data._id + i">
        <van-checkbox v-model="data.checked" />
        <img class="goods-img" :src="data.product.coverImg" alt="" />
        <div class="goods-info">
          <p>{{ data.product.name }}</p>
          <span>￥{{ data.product.price }}</span>
          <span @click="updatePro(data.product._id, 1)">+</span>
          {{ data.quantity }}
          <span @click="data.quantity > 1 && updatePro(data.product._id, -1)"
            >-</span
          >
          <span v-show="data.checked" @click="del(data._id)">删除</span>
        </div>
      </div>
    </div>
    <div v-else>购物车空空如也</div>
    <van-submit-bar
      :price="sumPrice * 100"
      button-text="提交订单"
      @submit="onSubmit"
    >
      <van-checkbox v-model="checked">全选</van-checkbox>
      <van-icon name="delete-o" v-show="checked" @click="dels" />
    </van-submit-bar>
  </div>
</template>

<script>
// checked写在computed里面
import {
  reqCartlistAPI,
  addToCart,
  reqDelcartAPI,
  reqDelcarstAPI,
} from "@/api/cart";
// import { reqSaveOrderAPI } from "@/api/order";
export default {
  data() {
    return {
      list: [],
    };
  },
  computed: {
    // 全选全不选
    checked: {
      set(flag) {
        console.log(flag);
        // 这个flag指的是全选的状态，如果为true那么全选，否则全部不选,this.$set给对象动态添加属性
        this.list.map((item) => this.$set(item, "checked", flag));
      },
      get() {
        // 如果购物车的总长度等于选中的总长度那么返回true，全选被选中
        // 这里面有两种情况1购物车有数据，2如果购物车里面没有数据的话，直接返回false，全选不选中
        if (this.list.length == 0) {
          return false;
        } else {
          return (
            this.list.length ==
            this.list.filter((item) => item.checked == true).length
          );
        }
      },
    },
    // 计算总价
    sumPrice() {
      return this.list
        .filter((item) => {
          return item.checked; //被选中项
        })
        .reduce(function (pre, cur) {
          return pre + cur.product.price * cur.quantity;
        }, 0);
    },
    // 获取选中的购车列表数据
    selectgoods() {
      var selectlist = [];
      this.list.forEach((item) => {
        // item.checked == true表示选中
        if (item.checked) {
          selectlist.push({
            quantity: item.quantity,
            product: item._id, //购物车的_id   product:{}
            price: item.product.price,
          });
        }
      });
      return selectlist;
    },
  },
  watch: {},

  methods: {
    async getcartlist() {
      const result = await reqCartlistAPI();
      console.log(result);
      this.list = result;
    },
    // 增加减少购物车
    async updatePro(id, num) {
      console.log(id, num);
      // 点击+或者-调用接口
      await addToCart(id, num);

      this.list.forEach((data) => {
        if (data.product._id == id) {
          data.quantity = data.quantity + num;
        }
      });
    },
    // 删除单个购物车信息
    async del(id) {
      const result = await reqDelcartAPI(id);
      console.log(result);
      // 删除之后重新调用一下接口
      this.getcartlist();
    },
    // 批量删除
    async dels() {
      // 获取所有选中的购物车的id,并且把他们组成数组
      console.log(1111, this.selectgoods);
      const ids = [];
      this.selectgoods.forEach((item) => {
        ids.push(item.product);
      });
      console.log(ids);
      const result = await reqDelcarstAPI(ids);
      console.log(result);
      this.getcartlist();
    },
    async onSubmit() {
      console.log("你点击了提交");
      this.$router.push("/address"); //跳转到添加收货人页面
    },
  },
  created() {
    this.getcartlist();
  },
  mounted() {},
  components: {},
};
</script>
<style scoped>
.goods {
  height: 100px;
  width: 100%;
  display: flex;
  justify-content: space-around;
  align-items: center;
  margin-bottom: 15px;
}
.goods-img {
  flex: 1;
  margin: 0 10px;
  height: 80px;
  border: 1px solid #ccc;
}
.goods-info {
  flex: 3;
}
.goods-info input {
  background-color: #fff;
}
.goods-info .goods-price {
  display: inline-block;
  width: 100px;
}
.goods-info .goods-delete {
  margin-left: 5px;
}
.goods-list {
  padding-bottom: 100px;
}
.van-submit-bar {
  position: fixed;
  bottom: 50px;
}
</style>
